package com.bawei.hgshop.controller;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bawei.hgshop.entity.Cart;
import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.entity.Sku;
import com.bawei.hgshop.entity.User;
import com.bawei.hgshop.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Reference
	UserService userService;
	
	
	
	

	@RequestMapping("getCheckCode") 
	@ResponseBody
	public MsgResult getCheckCode(HttpServletRequest request) {
			// 加入要求4位数字
			 int code =  new 	Random().nextInt(9000) + 1000;
			 System.err.println(" 验证码已经生成  ： 验证码是  ： " + code);
			 // 验证码保存到session当中
			 request.getSession().setAttribute("code", code+"");
			 request.getSession().setMaxInactiveInterval(60*5);//保存5分钟
			 
			 return new MsgResult(0, "ok",null);
			 
	} 
	
	@RequestMapping("tologin")
	public String toLogin(HttpServletRequest request) {
		return "login";
	}
	
	@RequestMapping("addcart") 
	public String  addcart(HttpServletRequest request,int skuId,int buyNum) {
		
		Cart cart = new Cart();
		cart.setPnum(buyNum);//购买数量
		//设置sku的
		Sku sku = new Sku();
		sku.setId(skuId);
		cart.setSku(sku);
		
		// 设置用户
		User loginUser = (User) request.getSession().getAttribute("loginuser");
		cart.setUid(loginUser.getUid());
		
		userService.addCart(cart);
		
		// 跳转到购物车列表
		return "redirect:cartlist";
		
		
	}
	
	@RequestMapping("createOrder")
	@ResponseBody
	public MsgResult createOrder(HttpServletRequest request,@RequestParam("cartIds[]") int[] cartIds ) {
		
		
		//
		// 设置用户
		User loginUser = (User) request.getSession().getAttribute("loginuser");
				
		userService.createOrder(loginUser.getUid(), cartIds);
		
		
		return new MsgResult(0,"",null);
	}
	
	/**
	 * 购物车的列表
	 * @param request  
	 * @return
	 */
	@RequestMapping("cartList")
	public String cartlist(HttpServletRequest request) {  
		
		User loginUser = (User) request.getSession().getAttribute("loginuser");
		//购物车的数据
		List<Cart> cartList=   userService.listCart(loginUser.getUid());
		
		cartList.forEach(x->{
			System.out.println("x is " + x);
		});
		request.setAttribute("cartList", cartList);
		return "cartList";
	}
	
	
	@RequestMapping("login")
	public String login(HttpServletRequest request,User user) {
		// 验证用户名密码是否正确
		User loginUser = userService.login(user);
		// 说明登录成功
		if(loginUser!=null) {
			request.getSession().setAttribute("loginuser", loginUser);
			return "redirect:../";
		}else {
			//登录失败 跳转到登录页面
			request.getSession().removeAttribute("loginuser");
			// 登录的数据需要回显
			request.setAttribute("user", user);
			return "login";
		}
	
	}
	
	
	/**
	 * 注册
	 * @return
	 */
	@RequestMapping("register")
	@ResponseBody
	public MsgResult register(HttpServletRequest request ,User user) {
		//校验短信验证码是否正确
		//从Session当中获取原来生成的那个验证码
		String smsCode = (String)request.getSession().getAttribute("code");
		// 验证码不对 或者还从来没有生成过
		if(smsCode==null || !smsCode.equals(user.getCode())) {
		return	new MsgResult(1, "验证码错误", null);
		}
		
		int register = userService.register(user);
		if(register>0)
			return  new MsgResult(0, "注册成功", null);
		else {
			return	new MsgResult(2, "注册失败", null);
		}
		
	}
}
